package com.dailyblue.java.io;

import java.io.*;

// 字符流
public class DemoH {

    // 字符流的读取
    private static void a1() throws IOException {
        File file = new File("D://a//a//a.txt");
        Reader reader = new FileReader(file);
        int ch;
        while ((ch = reader.read()) != -1) {
            char c = (char) (ch);
            System.out.print(c);
        }
        reader.close();
    }

    // 字符流的读取
    private static void a2() throws IOException {
        File file = new File("D://a//a//a.txt");
        Reader reader = new FileReader(file);
        int len;
        // 将读取到的内容存放到数组中
        char[] chars = new char[10];
        while ((len = reader.read(chars)) != -1) {
            System.out.print(chars);
        }
        reader.close();
    }

    // 带缓冲效果的字符流读取(只要涉及到文字的读取，推荐写法)
    private static void a3() throws IOException {
        File file = new File("D://a//a//a.txt");
        Reader reader = new FileReader(file);
        BufferedReader br = new BufferedReader(reader);
        while (true) {
            String line = br.readLine();
            if (line == null) {
                break;
            }
            System.out.println(line);
        }
    }

    // 字符流的写入
    private static void b1() throws IOException {
        File file = new File("D://a//a//b.txt");
        Writer writer = new FileWriter(file);
        // 其实字符流的写入也是自带缓冲效果
        writer.write("Hello,world!");
        // 需要关闭或者刷新
        writer.close();
    }

    private static void b2()throws IOException {
        File file = new File("D://a//a//b.txt");
        Writer writer = new FileWriter(file);
        BufferedWriter bw = new BufferedWriter(writer);
        bw.write("Hello,world!");
        bw.flush();
    }

    // 剪切文本文件，将D盘下的a.txt剪切到E盘中 字符流去做
    public static void main(String[] args) throws IOException {
        b2();
    }
}
